package org.finesys.chat.core.base.document.splitter;

import java.io.IOException;
import java.io.InputStream;

import org.finesys.chat.core.base.document.tokenizer.Tokenizer;

import opennlp.tools.sentdetect.SentenceDetectorME;
import opennlp.tools.sentdetect.SentenceModel;

public class DocumentBySentenceSplitter extends HierarchicalDocumentSplitter {

    private final SentenceModel sentenceModel = this.createSentenceModel();

    public DocumentBySentenceSplitter(int maxSegmentSizeInChars, int maxOverlapSizeInChars) {
        super(maxSegmentSizeInChars, maxOverlapSizeInChars, (Tokenizer) null, (DocumentSplitter) null);
    }

    public DocumentBySentenceSplitter(int maxSegmentSizeInChars, int maxOverlapSizeInChars, DocumentSplitter subSplitter) {
        super(maxSegmentSizeInChars, maxOverlapSizeInChars, (Tokenizer) null, subSplitter);
    }

    public DocumentBySentenceSplitter(int maxSegmentSizeInTokens, int maxOverlapSizeInTokens, Tokenizer tokenizer) {
        super(maxSegmentSizeInTokens, maxOverlapSizeInTokens, tokenizer, (DocumentSplitter) null);
    }

    public DocumentBySentenceSplitter(int maxSegmentSizeInTokens, int maxOverlapSizeInTokens, Tokenizer tokenizer, DocumentSplitter subSplitter) {
        super(maxSegmentSizeInTokens, maxOverlapSizeInTokens, tokenizer, subSplitter);
    }

    private SentenceModel createSentenceModel() {
        String sentenceModelFilePath = "/raw/opennlp/opennlp-en-ud-ewt-sentence-1.0-1.9.3.bin";
        try {
            InputStream is = this.getClass().getResourceAsStream(sentenceModelFilePath);
            SentenceModel sentenceModel;
            try {
                sentenceModel = new SentenceModel(is);
            } catch (IOException e) {
                throw new RuntimeException(e);
            } finally {
                if (is != null) {
                    is.close();
                }
            }
            return sentenceModel;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public String[] split(String text) {
        SentenceDetectorME sentenceDetector = new SentenceDetectorME(this.sentenceModel);
        return sentenceDetector.sentDetect(text);
    }

    public String joinDelimiter() {
        return " ";
    }

    protected DocumentSplitter defaultSubSplitter() {
        return new DocumentByWordSplitter(this.maxSegmentSize, this.maxOverlapSize, this.tokenizer);
    }
}
